<template>
  <view class="container">
    <view class="ad-section">
      <!-- <text class="title">开屏广告测试</text> -->
      <!-- <uni-ad  style="width: 100%; height: 100%;"
        adpid="1121750853"
        type="splash"
        @load="onLoad"
        @close="onClose"
        @error="onError"
      ></uni-ad> -->

        <!-- <ad-rewarded-video adpid="1249227735" :loadnext="true" v-slot:default="{loading, error}"
        @load="onLoad" @close="onClose" @error="onError">
          <button :disabled="loading" :loading="loading">显示广告</button>
          <view v-if="error">{{error}}</view>
        </ad-rewarded-video> -->

   <ad-interstitial adpid="1121750853" 
   :loadnext="true" v-slot:default="{loading, error}" @load="onLoad" @close="onClose" @error="onError">
      <button :disabled="loading" :loading="loading">显示广告</button>
      <view v-if="error">{{error}}</view>
    </ad-interstitial>

      <!-- <ad-interstitial adpid="1121750853" 
        :loadnext="true" 
        v-slot:default="{loading, error}"
        @load="onLoad" 
        @close="onClose" @error="onError">
          <button :disabled="loading" :loading="loading">显示广告</button>
          <view v-if="error">{{error}}</view>
      </ad-interstitial> -->


      <button @click="goToMain" class="skip-btn">跳过广告</button>
      
      <view class="countdown" v-if="showCountdown">
        {{countdown}}秒后自动跳转
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      showCountdown: false,
      countdown: 5,
      countdownTimer: null
    };
  },
  
  onLoad() {
    // 3秒后显示倒计时
    setTimeout(() => {
      this.showCountdown = true;
      this.startCountdown();
    }, 3000);
  },
  
  onUnload() {
    // 清理定时器
    this.clearTimers();
  },
  
  methods: {
    onLoad(e) {
      console.log('广告加载成功', e);
      uni.showToast({
        title: '广告加载成功',
        icon: 'success'
      });
    },
    
    onClose(e) {
      console.log('广告关闭', e);
      uni.showToast({
        title: '广告已关闭',
        icon: 'none'
      });
      this.goToMain();
    },
    
    onError(e) {
      console.log('广告错误', e);
      uni.showToast({
        title: '广告加载失败',
        icon: 'none'
      });
      this.goToMain();
    },
    
    // 启动倒计时
    startCountdown() {
      this.countdownTimer = setInterval(() => {
        this.countdown--;
        if (this.countdown <= 0) {
          this.goToMain();
        }
      }, 1000);
    },
    
    // 清理定时器
    clearTimers() {
      if (this.countdownTimer) {
        clearInterval(this.countdownTimer);
      }
    },
    
    goToMain() {
      // 清理定时器
      this.clearTimers();
      
      // 跳转到主页面
      // uni.redirectTo({
      //   url: '/pages/mbit/index'
      // });
    }
  }
};
</script>

<style scoped>
.container {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #f5f5f5;
}

.ad-section {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 50rpx;
  color: #333;
}

.skip-btn {
  position: absolute;
  top: 100rpx;
  right: 50rpx;
  width: 120rpx;
  height: 60rpx;
  background-color: rgba(0, 0, 0, 0.6);
  border-radius: 30rpx;
  color: #ffffff;
  font-size: 24rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  border: none;
}

.countdown {
  position: absolute;
  bottom: 100rpx;
  font-size: 28rpx;
  color: #666;
}
</style>